<?php

/**
 * This is the model class for table "product_image".
 *
 * The followings are the available columns in table 'product_image':
 * @property integer $identification
 * @property integer $product
 * @property string $name
 * @property string $url
 * @property string $types
 * @property string $tooltip
 * @property string $note
 * @property string $insert
 * @property string $update
 * @property string $delete
 *
 * The followings are the available model relations:
 * @property Product $product0
 */
class ProductImage extends CActiveRecord
{
    public $key;
    public $product_name;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ProductImage the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'product_image';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('product, name, url, types, insert', 'required'),
            array('product', 'numerical', 'integerOnly'=>true),
            array('name, url, tooltip', 'length', 'max'=>255),
            array('types', 'length', 'max'=>10),
            array('note, update, delete', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('product, name, url, types, tooltip, note, insert, update, delete', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'product0' => array(self::BELONGS_TO, 'Product', 'product'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'identification' => 'Identification',
            'product' => 'Product',
            'name' => 'Name',
            'url' => 'Url',
            'types' => 'Types',
            'tooltip' => 'Tooltip',
            'note' => 'Note',
            'insert' => 'Insert',
            'update' => 'Update',
            'delete' => 'Delete',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('product',$this->product);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('types',$this->types,true);
        $criteria->compare('tooltip',$this->tooltip,true);
        $criteria->compare('note',$this->note,true);
        $criteria->compare('insert',$this->insert,true);
        $criteria->compare('update',$this->update,true);
        $criteria->compare('delete',$this->delete,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


    /** addDataToDb =============================
     * @author   TuanNguyen
     * @desc     save product, product image entity to database
     * @param    string $p_insertStaement: string for values in insert statement
     * @return   boolean
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function getDataByProductID($i_productID)
    {
        //Set SQL command
        $command = Yii::app()->db->createCommand("CALL productImage_06_getProduct(:p_productID)");

        //Set parameter for SQL command
        $command->bindValue("p_productID", $i_productID,PDO::PARAM_INT);

        try
        {
            $arr_model = array();
            $arr_data  = $command->queryAll();

            foreach ( $arr_data as $key => $row)
            {
                $m_model = new ProductImage();
                $m_model->unsetAttributes();

                $m_model->identification    = $row["identification"];
                $m_model->key               = $row["identification"];
                $m_model->product           = $row["product"];
                $m_model->name              = $row["name"];
                $m_model->url               = $row["url"];
                $m_model->types             = $row["types"];
                $m_model->tooltip           = $row["tooltip"];
                $m_model->note              = $row["note"];
                $m_model->insert            = $row["insert"];
                $m_model->update            = $row["update"];
                $m_model->delete            = $row["delete"];

                array_push($arr_model, $m_model);
            }//End foreach

            return $arr_model;
        }
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_2";
            $o_error->param     = array("SQLException" => $ex->getMessage());
            return $o_error;
        }
    }//End getDataByID


    /** deleteData =============================
     * @author   TuanNguyen
     * @desc     delete record by where clause
     * @param    int $i_ID: id of record
     * @return   boolean
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function deleteData($s_whereClause)
    {
        //Set SQL command
        $command = Yii::app()->db->createCommand("CALL productImage_05_delete (:in_where)");

        //Set parameter for SQL command
        $command->bindValue("in_where",$s_whereClause);

        try
        {
            $i_result = $command->execute();

            if( $i_result >= 0)
                return true;
            else 
            {
                $o_error            = new ErrorEntity();
                $o_error->errCode   = "ERR_1";
                $o_error->param     = null;
                return $o_error;
            }
        }
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_2";
            $o_error->param     = array("SQLException" => $ex->getMessage());
            return $o_error;
        }
    }//End deleteData
}